<?php



namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Issue;
use App\Models\User;
use Illuminate\Support\Facades\Log;



/**
 * @OA\Tag(
 *      name="Issue",
 *      description="Issue 管理",
 * )
 */

/**
 * @OA\Schema(
 *     schema="PaginatedIssueResponse",
 *     type="object",
 *     @OA\Property(property="current_page", type="integer", example=1),
 *     @OA\Property(property="data", type="array", @OA\Items(ref="#/components/schemas/Issue")),
 *     @OA\Property(property="first_page_url", type="string", example="http://localhost/api/issues?page=1"),
 *     @OA\Property(property="from", type="integer", example=1),
 *     @OA\Property(property="last_page", type="integer", example=1),
 *     @OA\Property(property="last_page_url", type="string", example="http://localhost/api/issues?page=1"),
 *     @OA\Property(property="links", type="array", @OA\Items(
 *         @OA\Property(property="url", type="string", nullable=true),
 *         @OA\Property(property="label", type="string"),
 *         @OA\Property(property="active", type="boolean"),
 *     )),
 *     @OA\Property(property="next_page_url", type="string", nullable=true),
 *     @OA\Property(property="path", type="string", example="http://localhost/api/issues"),
 *     @OA\Property(property="per_page", type="integer", example=10),
 *     @OA\Property(property="prev_page_url", type="string", nullable=true),
 *     @OA\Property(property="to", type="integer", example=1),
 *     @OA\Property(property="total", type="integer", example=1),
 * )
 */


class IssueController extends Controller
{

    /**
     * @OA\Post(
     *      path="/api/issues",
     *      summary="创建 issue",
     *      tags={"Issue"},
     *      security={{"bearerAuth":{}}},
     *      @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              required={"title","description"},
     *              @OA\Property(property="title", type="string", example="title 002"),
     *              @OA\Property(property="description", type="string", example="description 002"),
     *          ),
     *      ),
     *      @OA\Response(
     *          response=200,
     *          description="执行成功",
     *          @OA\JsonContent(
     *              @OA\Property(property="message", type="string", example="OK"),
     *              @OA\Property(property="issue", ref="#/components/schemas/Issue"),
     *          )
     *      ),
     *     @OA\Response(
     *         response=401,
     *         description="未授权",
     *     ),
     * )
     */

    public function createIssue(Request $request)
    {
        $request->validate([
            'title' => 'required|string',
            'description' => 'required|string',
            'assigned_to' => 'nullable|exists:users,id'
        ]);

        LOG::debug(sprintf("createIssue start\n"));

        $user = auth()->user();
        LOG::debug(sprintf("user=%s\n", json_encode($user)));


        $issue = Issue::create([
            'title' => $request->title,
            'description' => $request->description,
            'user_id' => $user->id,
            'assigned_to' => $request->assigned_to
        ]);
        // $issue = '11111';

        LOG::debug(sprintf("createIssue||request=%s\n", json_encode($request->title)));

        return response()->json(['message' => 'OK', 'issue' => $issue], 200);
    }

    /**
     * @OA\Get(
     *     path="/api/issues",
     *     tags={"Issue"},
     *     summary="issue批量查询",
     *     description="查出所有的 issue，有分页",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(
     *         name="page",
     *         in="query",
     *         required=false,
     *         description="Page number",
     *         @OA\Schema(type="integer", example=1)
     *     ),
     *     @OA\Parameter(
     *         name="per_page",
     *         in="query",
     *         required=false,
     *         description="Items per page",
     *         @OA\Schema(type="integer", example=10)
     *     ),
     *     @OA\Response(
     *         response="200",
     *         description="List of issues with pagination",
     *         @OA\JsonContent(ref="#/components/schemas/PaginatedIssueResponse")
     *     ),
     *     @OA\Response(
     *         response=401,
     *         description="未授权",
     *     ),
     * )
     */

    public function listIssues(Request $request)
    {
        // 从请求中获取 page 参数，默认为 1
        $page = $request->input('page', 1);
        // 从请求中获取 per_page 参数，默认为 10
        $perPage = $request->input('per_page', 3);

        $issues = Issue::with('comments')->paginate($perPage, ['*'], 'page', $page);

        $user = auth()->user();
        LOG::debug(sprintf("listIssues|user=%s|page=%d|perPage=%d", json_encode($user), $page, $perPage));
        LOG::debug(sprintf("issues=%s", json_encode($issues)));

        return response()->json($issues);
    }

}
